package subumbo.demo.view.wishform {
	import flash.events.MouseEvent;
	
	import mx.controls.Alert;
	
	import org.robotlegs.mvcs.Mediator;
	
	import subumbo.demo.controller.signal.PreviewSignal;
	import subumbo.demo.model.CopyModel;
	import subumbo.demo.model.WishModel;

	public class WishFormMediator extends Mediator {
		
		[Inject] public var _view:WishForm;
		[Inject] public var copyModel:CopyModel;
		[Inject] public var whishModel:WishModel;
		[Inject] public var preview:PreviewSignal;
		
		public override function onRegister():void {
			_view.copyModel = copyModel.whishForm;
			_view.submitBtn.addEventListener(MouseEvent.MOUSE_DOWN, onSubmit);
		}
		
		private function onSubmit(e:MouseEvent):void {
			
			if(_view.w1.text == "" && _view.w2.text == "" && _view.w3.text == ""
			&& _view.w4.text == "" && _view.w5.text == "" && _view.w6.text == "") {
				Alert.show("Please enter at least one wish.", "Oops");
				return;		
			}
			
			whishModel.wish1 = _view.w1.text;
			whishModel.wish2 = _view.w2.text;
			whishModel.wish3 = _view.w3.text;
			whishModel.wish4 = _view.w4.text;
			whishModel.wish5 = _view.w5.text;
			whishModel.wish6 = _view.w6.text;
			preview.dispatch();
		}
		
		public override function onRemove():void {
			_view.copyModel = null;
			_view.submitBtn.removeEventListener(MouseEvent.MOUSE_DOWN, onSubmit);
		}
	}
}